﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace search
{
	/// <summary>
	/// AddArticle 的摘要说明。
	/// </summary>
	public partial class AddArticle : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			HttpCookie nameCookie = Request.Cookies["NickName"];
			if(nameCookie == null)
				Response.Redirect("../client/index.aspx");
			this.Label1.Text = nameCookie.Value + "，欢迎您发表文章。";

//			if(Session["Email"] == null)
//				Response.Redirect("login.aspx");
//			this.Label1.Text = Session["NickName"].ToString() + "，欢迎您发表文章。";

			if(!Page.IsPostBack)
			{
				CheckUser();

				this.TextBox_Chapter0.Attributes["onkeydown"] = "return check2(event); ";
				this.TextBox_Section0.Attributes["onkeydown"] = "return check3(event);";
				this.TextBox_Chapter0.Attributes["onkeyup"] = "document.all('TextBox_Chapter0_To').value=document.all('TextBox_Chapter0').value; ";
				this.TextBox_Section0.Attributes["onkeyup"] = "document.all('TextBox_Section0_To').value=document.all('TextBox_Section0').value; ";
				this.TextBox_Chapter0_To.Attributes["onkeypress"] = "return check45(event);";
				this.TextBox_Section0_To.Attributes["onkeypress"] = "return check45(event);";
				this.Button_Submit.Attributes["onclick"] = "return checkinput();";
				BindDropDownList();
				BindListBox();
				BindListBoxSub();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private void CheckUser()
		{
			
			this.Literal_Admin.Text = "";
			HttpCookie emailCookie = Request.Cookies["Email"];
			if(emailCookie == null)
			{
				return;
			}
			DataSet ds = SearchDataAccess.GetUserRoles(emailCookie.Value);
			ds.Tables[0].DefaultView.RowFilter = "RoleID=4";
			if(ds.Tables[0].DefaultView.Count > 0)
			{
				this.Literal_Admin.Text = "<FONT color='#ffffff'>&nbsp;| </FONT><A href='AdminArticle.aspx'><FONT color='#ffffff'>发帖审核</FONT></A>";
			}
		}



		private void BindDropDownList()
		{
			DataSet ds = BibleToolsDataAccess.GetBibleTitles();
			this.DropDownList1.DataTextField = "Title_CN";
			this.DropDownList1.DataValueField = "Title_CN";
			this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
			this.DropDownList1.DataBind();
		}


		private void BindListBox()
		{
//			dsTopics = new DataSet();
//			string filename = Server.MapPath(".") + "\\topics.xml";
//			dsTopics.ReadXml(filename);

			DataSet ds = SearchDataAccess.GetTopic();

			this.ListBox1.DataSource = ds.Tables[0].DefaultView;
			this.ListBox1.DataTextField = "Topic";
			this.ListBox1.DataValueField = "Topic";
			this.ListBox1.DataBind();
			
			this.ListBox1.SelectedIndex = 0;
		}

		private void BindListBoxSub()
		{
			DataSet ds = SearchDataAccess.GetTopic();

			ds.Tables[0].DefaultView.RowFilter = "Topic='" + this.ListBox1.SelectedValue + "'";
			this.ListBox2.Items.Clear();
			if(ds.Tables[0].DefaultView.Count > 0)
			{
				string[] arrsubtopics = ds.Tables[0].DefaultView[0].Row["SubTopic"].ToString().Split(',');
				this.ListBox2.DataSource = arrsubtopics;
				this.ListBox2.DataBind();
				this.ListBox2.SelectedIndex = 0;
			}
		}

		protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
        {        
			BindListBoxSub();
		}

        protected void Button_Submit_Click1(object sender, EventArgs e)
        {

       		HttpCookie emailCookie = Request.Cookies["Email"];
			if(emailCookie == null)
				Response.Redirect("../client/index.aspx");
			DataSet ds = SearchDataAccess.GetUser(emailCookie.Value);

			int UserID = Convert.ToInt32(ds.Tables[0].Rows[0]["UserID"]);

			int postid = -1;
			if(this.ListBox_Title.Items.Count == 0)
			{
				if(!CheckInput1())
					return;

				if(!CheckInput())
					return;

				postid = SearchDataAccess.InsertArticle(UserID, this.TextBox1.Text.Trim(), this.TextBox6.Text.Trim(),this.DropDownList1.SelectedValue,
					Convert.ToInt32(this.TextBox_Chapter0.Text.Trim()), Convert.ToInt32(this.TextBox_Section0.Text.Trim()),
					Convert.ToInt32(this.TextBox_Chapter0_To.Text.Trim()), Convert.ToInt32(this.TextBox_Section0_To.Text.Trim()),
					this.ListBox1.SelectedValue, this.ListBox2.SelectedValue, this.TextBox_Url.Text.Trim());
			}
			else
			{
				if(!CheckInput())
					return;

				string[] s = this.ListBox_Title.Items[0].Value.Split(',');

				postid = SearchDataAccess.InsertArticle(UserID, this.TextBox1.Text.Trim(), this.TextBox6.Text.Trim(),s[0],
					Convert.ToInt32(s[1]), Convert.ToInt32(s[2]),
					Convert.ToInt32(s[3]), Convert.ToInt32(s[4]),
					this.ListBox1.SelectedValue, this.ListBox2.SelectedValue, this.TextBox_Url.Text.Trim());
				for(int i = 1; i < this.ListBox_Title.Items.Count; i++)
				{
					string[] ss = this.ListBox_Title.Items[i].Value.Split(',');
					SearchDataAccess.InsertPostBible(postid, ss[0], Convert.ToInt32(ss[1]), 
						Convert.ToInt32(ss[2]),
						Convert.ToInt32(ss[3]), 
						Convert.ToInt32(ss[4]));
				}
			}

			if(postid > 0)
			{

				Response.Redirect("EditArticle.aspx");
			}
			else
			{
				this.Literal_script.Text = "<script defer>alert('发布失败！');</script>";
			}

		}


		private bool CheckInput1()
		{
			string s = this.TextBox_Chapter0.Text.Trim();

			if(s.Length == 0)
			{
				this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Chapter0').focus();</script>";
				return false;
			}
			else
			{
				try
				{
					int chap1 = Convert.ToInt32(s);
				}
				catch
				{
					this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Chapter0').focus();</script>";
					return false;
				}
			}

			s = this.TextBox_Section0.Text.Trim();
			if(s.Length == 0)
			{
				this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Section0').focus();</script>";
				return false;
			}
			else
			{
				try
				{
					int chap1 = Convert.ToInt32(s);
				}
				catch
				{
					this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Section0').focus();</script>";
					return false;
				}
			}

			s = this.TextBox_Chapter0_To.Text.Trim();
			if(s.Length == 0)
			{
				this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Chapter0_To').focus();</script>";
				return false;
			}
			else
			{
				try
				{
					int chap1 = Convert.ToInt32(s);
				}
				catch
				{
					this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Chapter0_To').focus();</script>";
					return false;
				}
			}
			
			s = this.TextBox_Section0_To.Text.Trim();
			if(s.Length == 0)
			{
				this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Section0_To').focus();</script>";
				return false;
			}
			else
			{
				try
				{
					int chap1 = Convert.ToInt32(s);
				}
				catch
				{
					this.Literal_script.Text = "<script defer>alert('请输入经文章节！');document.all('TextBox_Section0_To').focus();</script>";
					return false;
				}
			}
			return true;
		}

		private bool CheckInput()
		{
			if(this.TextBox1.Text.Trim() == "")
			{
				this.Literal_script.Text = "<script defer>alert('请输入标题！');document.all('TextBox1').focus();</script>";
				return false;
			}

			if(this.TextBox6.Text.Trim() == "" && this.TextBox_Url.Text.Trim() == "")
			{
				this.Literal_script.Text = "<script defer>alert('请输入文章内容或Url！');document.all('TextBox6').focus();</script>";
				return false;
			}

			return true;
		}
		
	
        protected void Button1_Click1(object sender, EventArgs e)
        {
            if (!CheckInput1())
                return;

            string t = this.DropDownList1.SelectedValue + "  " + this.TextBox_Chapter0.Text.Trim() + "∶" + this.TextBox_Section0.Text.Trim() +
                          "—" + this.TextBox_Chapter0_To.Text.Trim() + "∶" + this.TextBox_Section0_To.Text.Trim();
            string v = this.DropDownList1.SelectedValue + "," + this.TextBox_Chapter0.Text.Trim() + "," + this.TextBox_Section0.Text.Trim() +
                "," + this.TextBox_Chapter0_To.Text.Trim() + "," + this.TextBox_Section0_To.Text.Trim();
            ListItem listitem = new ListItem(t, v);
            this.ListBox_Title.Items.Add(listitem);
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            if (this.ListBox_Title.SelectedIndex > -1)
            {
                this.ListBox_Title.Items.Remove(this.ListBox_Title.SelectedItem);
            }

        }

       

       
	}
}
